ft = {};

ft.callbackFail = function(err) {
	console.log(err);
};

ft.service = function(controller, method, args, success, fail) {
	$.ajax({
		url : '/' + controller + '/' + method,
		type : "POST",
		data : {
			json : JSON.stringify(args)
		},
		dataType : "json",
		beforeSend : function(x) {
			if (x && x.overrideMimeType) {
				x.overrideMimeType("application/j-son;charset=UTF-8");
			}
		},
		success : function(ret) {
			if (ret && ret.errorInfo) {
				if (!fail) {
					fail = ft.callbackFail;
				}
				fail(ret);
			} else {
				if (success) {
					success(ret);
				}
			}
		}
	});
};

ft.redirect = function(url) {
	location.href = url;
};

/* Limpa os campos de um formulario */
ft.ResetForm = function(form) {
	$(form).find(':input').each(function() {
		switch (this.type) {
		case 'password':
		case 'select-multiple':
		case 'select-one':
		case 'text':
		case 'email':
		case 'textarea':
			$(this).val('');
			break;
		case 'checkbox':
		case 'radio':
			this.checked = false;
		}
	});
};

ft.FecharModal = function(container){
	$('#mask, .window').hide();
};

ft.Modal = function(container, fechar) {
	// armazena o atributo href do link
	var id = container;
	// armazena a largura e a altura da tela
	var maskHeight = $(document).height();
	var maskWidth = $(window).width();

	// Define largura e altura do div#mask iguais ás dimensões da tela
	$('#mask').css({
		'width' : maskWidth,
		'height' : maskHeight
	});

	// efeito de transição
	$('#mask').fadeIn(1000);
	$('#mask').fadeTo("slow", 0.8);

	// armazena a largura e a altura da janela
	var winH = $(window).height();
	var winW = $(window).width();
	// centraliza na tela a janela popup
	$(id).css('top', winH / 2 - $(id).height() / 2);
	$(id).css('left', winW / 2 - $(id).width() / 2);
	// efeito de transição
	$(id).fadeIn(2000);

	if(fechar){
		$('.close').show();
	}else{
		$('.close').hide();
	}
	$('.window .close').click(function(e) {
		// cancela o comportamento padrão do link
		e.preventDefault();
		$('#mask, .window').hide();
	});

	// se div#mask for clicado
	$('#mask').click(function() {
		$(this).hide();
		$('.window').hide();
	});
};

jQuery.serializeForm = function(form) {
	$(form).find("input").each(function() {
		$(this).removeAttr('disabled');
	});

	var json = {};

	json = $(form).serializeObject();
	// PLUGIN SERIALIZE OBJECT

	$(form + ' input[type="checkbox"]').each(function() {
		json[$(this).attr('id')] = ($(this).is(':checked')) ? 1 : 0;
	});

	$(form + ' .easyui-combobox').each(function(a, b) {
		var idElemento = $('#' + $(this).attr('id'));
		json[$(this).attr('id')] = idElemento.combobox('getValue');
	});

	$(form + ' .easyui-datebox').each(function() {
		var idElemento = $('#' + $(this).attr('id'));
		json[$(this).attr('id')] = idElemento.datebox('getValue');
	});

	$(form + ' .easyui-datetimebox').each(function() {
		var idElemento = $('#' + $(this).attr('id'));
		json[$(this).attr('id')] = idElemento.datetimebox('getValue');
	});

	$(form + ' .easyui-numberbox').each(function() {
		var idElemento = $('#' + $(this).attr('id'));
		json[$(this).attr('id')] = idElemento.numberbox('getValue');
	});

	$(form + ' .easyui-numberspinner').each(function() {
		var idElemento = $('#' + $(this).attr('id'));
		json[$(this).attr('id')] = idElemento.numberspinner('getValue');
	});
	return json;
};